package com.yds.developtools.utils

import android.graphics.drawable.Drawable
import android.text.TextUtils
import android.view.View
import androidx.appcompat.widget.AppCompatTextView
import androidx.databinding.BindingAdapter
/**
 * 作者:     YDS
 * 创建时间: 2025/3/19 14:45
 * 页面描述: 图片、文字绑定
 */
object YDSBindingAdapters {

//    @BindingAdapter(value = ["imageUrl"])
//    @JvmStatic
//    fun loadImageUrl(imageView: AppCompatImageView, imageUrl: String?) {
//        Glide.with(imageView.context)
//            .load(imageUrl)
//            .placeholder(R.mipmap.ic_launcher_round)
//            .error(R.mipmap.ic_launcher_round)
//            .into(imageView)
//    }
//
//    @BindingAdapter(value = ["imageBitmap"])
//    @JvmStatic
//    fun loadImageBitmap(imageView: AppCompatImageView, imageBitmap: Bitmap?) {
//        Glide.with(imageView.context)
//            .load(imageBitmap)
//            .placeholder(R.mipmap.ic_launcher_round)
//            .error(R.mipmap.ic_launcher_round)
//            .into(imageView)
//    }

    @BindingAdapter(value = ["text", "default"], requireAll = false)
    @JvmStatic
    fun setText(textView: AppCompatTextView, text: String?, default: String?) {
        textView.text = if (TextUtils.isEmpty(text)) default else text
    }

    @BindingAdapter(value = ["drawableStart"])
    @JvmStatic
    fun setStartDrawable(textView: AppCompatTextView, drawableStart: Drawable?) {
        if (null == drawableStart) {
            textView.setCompoundDrawables(null, null, null, null)
        } else {
            drawableStart.setBounds(0, 0, drawableStart.minimumWidth, drawableStart.minimumHeight)
            textView.setCompoundDrawables(drawableStart, null, null, null)
        }
    }

    @BindingAdapter(value = ["drawableEnd"])
    @JvmStatic
    fun setEndDrawable(textView: AppCompatTextView, drawableEnd: Drawable?) {
        if (null == drawableEnd) {
            textView.setCompoundDrawables(null, null, null, null)
        } else {
            drawableEnd.setBounds(0, 0, drawableEnd.minimumWidth, drawableEnd.minimumHeight)
            textView.setCompoundDrawables(drawableEnd, null, null, null)
        }
    }

    @BindingAdapter(value = ["visibility"])
    @JvmStatic
    fun setViewInvisible(view: View, visibility: Boolean) {
        view.visibility = if (visibility) View.VISIBLE else View.INVISIBLE
    }

    @BindingAdapter(value = ["visibility"])
    @JvmStatic
    fun setViewGone(view: View, visibility: Boolean) {
        view.visibility = if (visibility) View.VISIBLE else View.GONE
    }

    /*@BindingAdapter(value = ["onClick"])
    @JvmStatic
    fun setOnClick(view: View, action: () -> Unit) {
        view.onClick { action() }
    }*/

    private var startTime: Long = 0

    @BindingAdapter(value = ["onClick"], requireAll = false)
    @JvmStatic
    fun setOnClick(view: View, listener: View.OnClickListener?) {
        view.setOnClickListener {
            val currentTimeMillis = System.currentTimeMillis()
            if (currentTimeMillis - startTime >= 1200) {
                startTime = currentTimeMillis
                listener?.onClick(view)
            }
        }
    }

    @BindingAdapter(value = ["layoutHeight"])
    @JvmStatic
    fun setViewHeight(view: View, layoutHeight: Float) {
        val height = view.layoutParams.height
        if (height != layoutHeight.toInt()) {
            view.apply {
                this.layoutParams = layoutParams.apply {
                    this.height = layoutHeight.toInt()
                }
            }
        }
    }


}